import { director, ResolutionPolicy, UITransform, v2, v3, Vec2, view } from "cc";

export default class GameFun {
    private static _instance: GameFun | null = null;

    public static get Instance() {
        return GameFun._instance || (GameFun._instance = new GameFun());
    }
    
    //屏幕竖屏适配
    public static PortraitAdaptation() {
        let size = view.getFrameSize();
        let num = size.width / size.height;
        if (num < 0.5625) {
            //view.setResolutionPolicy(ResolutionPolicy.FIXED_WIDTH);
            view.setDesignResolutionSize(720, 1280, ResolutionPolicy.FIXED_WIDTH);
        } else {
            view.setDesignResolutionSize(720, 1280, ResolutionPolicy.FIXED_HEIGHT);
        }
    }

    /**
     * 随机整数,不包括max
     */
    public static randomInt = function (min, max) {
        let num = Math.floor(Math.random() * (max - min)) + min;
        // cc.log("randomInt min = " + min + " , max = " + max + " , num = " + num);
        return num;
    };

    /**
     *  * 把一个节点的本地坐标转到另一个节点的本地坐标下
     * @param {*} node 
     * @param {*} targetNode 
     */
    public static ConvetOtherNodeSpaceAR(node, targetNode) {
        if (!node || !targetNode) {
            return null;
        }
        //先转成世界坐标
        let worldPoint:Vec2 =node.worldPosition;
        let pos = targetNode.getComponent(UITransform)!.convertToNodeSpaceAR(v3(worldPoint.x, worldPoint.y, 0));
        return pos;
    }

    /**
     * 复制数组
     */
    public static CopyArray(arr)
    {
        return JSON.parse( JSON.stringify( arr ) )
    }

    /**
     * 随机打乱数组
     */
    public static UpsetArray(arr)
    {
        var newArr =this.CopyArray(arr)
        var randomNumber = function(){
            return 0.5 - Math.random()
        }
        newArr.sort(randomNumber)
        return newArr
    }

    public static SplitVector3(n:string, m:string){
        var list = n.split(m);
        var ve3=v3(Number.parseFloat(list[0]),Number.parseFloat(list[1]),Number.parseFloat(list[2])); 
        return ve3;
    }

    public static SplitString(n:string, m:string){
        var list = n.split(m);
        return list;
    }
}